package com.victor.config.properties;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.sql.SQLException;


/**
 * 数据库数据源配置
 * 说明:这个类中包含了许多默认配置,若这些配置符合您的情况,您可以不用管,若不符合,建议不要修改本类,建议直接在"application.yml"中配置即可
 */
@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class DruidProperties {
    /**
     * url
     */
    private String url = "";
    /**
     * username
     */
    private String username = "";
    /**
     * password
     */
    private String password = "";
    /**
     * driverClassName
     */
    private String driverClassName = "com.mysql.jdbc.Driver";
    /**
     * initialSize
     */
    private Integer initialSize = 2;
    /**
     * minIdle
     */
    private Integer minIdle = 1;
    /**
     * maxActive
     */
    private Integer maxActive = 20;
    /**
     * maxWait
     */
    private Integer maxWait = 60000;
    /**
     * timeBetweenEvictionRunsMillis
     */
    private Integer timeBetweenEvictionRunsMillis = 60000;
    /**
     * minEvictableIdleTimeMillis
     */
    private Integer minEvictableIdleTimeMillis = 300000;
    /**
     * validationQuery
     */
    private String validationQuery = "SELECT 'x'";
    /**
     * testWhileIdle
     */
    private Boolean testWhileIdle = true;
    /**
     * testOnBorrow
     */
    private Boolean testOnBorrow = false;
    /**
     * testOnReturn
     */
    private Boolean testOnReturn = false;
    /**
     * poolPreparedStatements
     */
    private Boolean poolPreparedStatements = true;
    /**
     * maxPoolPreparedStatementPerConnectionSize
     */
    private Integer maxPoolPreparedStatementPerConnectionSize = 20;
    /**
     * filters
     */
    private String filters = "stat";

    /**
     * <b>Description:初始化数据源</b><br>
     *
     * @param dataSource
     * @Note <b>Author:</b>zhaojiabin</a>
     * <br><b>Date:</b> 2017年10月18日 上午10:13:04
     * <br><b>Version:</b> 1.0
     */
    public void config(DruidDataSource dataSource) {
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        dataSource.setDriverClassName(driverClassName);
        /**
         * 定义初始连接数
         */
        dataSource.setInitialSize(initialSize);
        /**
         * 最小空闲
         */
        dataSource.setMinIdle(minIdle);
        /**
         * 定义最大连接数
         */
        dataSource.setMaxActive(maxActive);
        /**
         * 最长等待时间
         */
        dataSource.setMaxWait(maxWait);

        /**
         * 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
         */
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);

        /**
         *  配置一个连接在池中最小生存的时间，单位是毫秒
         */
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        dataSource.setValidationQuery(validationQuery);
        dataSource.setTestWhileIdle(testWhileIdle);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestOnReturn(testOnReturn);

        /**
         * 打开PSCache，并且指定每个连接上PSCache的大小
         */
        dataSource.setPoolPreparedStatements(poolPreparedStatements);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);

        try {
            dataSource.setFilters(filters);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public Integer getInitialSize() {
        return initialSize;
    }

    public void setInitialSize(Integer initialSize) {
        this.initialSize = initialSize;
    }

    public Integer getMinIdle() {
        return minIdle;
    }

    public void setMinIdle(Integer minIdle) {
        this.minIdle = minIdle;
    }

    public Integer getMaxActive() {
        return maxActive;
    }

    public void setMaxActive(Integer maxActive) {
        this.maxActive = maxActive;
    }

    public Integer getMaxWait() {
        return maxWait;
    }

    public void setMaxWait(Integer maxWait) {
        this.maxWait = maxWait;
    }

    public Integer getTimeBetweenEvictionRunsMillis() {
        return timeBetweenEvictionRunsMillis;
    }

    public void setTimeBetweenEvictionRunsMillis(Integer timeBetweenEvictionRunsMillis) {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    }

    public Integer getMinEvictableIdleTimeMillis() {
        return minEvictableIdleTimeMillis;
    }

    public void setMinEvictableIdleTimeMillis(Integer minEvictableIdleTimeMillis) {
        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    }

    public String getValidationQuery() {
        return validationQuery;
    }

    public void setValidationQuery(String validationQuery) {
        this.validationQuery = validationQuery;
    }

    public Boolean getTestWhileIdle() {
        return testWhileIdle;
    }

    public void setTestWhileIdle(Boolean testWhileIdle) {
        this.testWhileIdle = testWhileIdle;
    }

    public Boolean getTestOnBorrow() {
        return testOnBorrow;
    }

    public void setTestOnBorrow(Boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }

    public Boolean getTestOnReturn() {
        return testOnReturn;
    }

    public void setTestOnReturn(Boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
    }

    public Boolean getPoolPreparedStatements() {
        return poolPreparedStatements;
    }

    public void setPoolPreparedStatements(Boolean poolPreparedStatements) {
        this.poolPreparedStatements = poolPreparedStatements;
    }

    public Integer getMaxPoolPreparedStatementPerConnectionSize() {
        return maxPoolPreparedStatementPerConnectionSize;
    }

    public void setMaxPoolPreparedStatementPerConnectionSize(Integer maxPoolPreparedStatementPerConnectionSize) {
        this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
    }

    public String getFilters() {
        return filters;
    }

    public void setFilters(String filters) {
        this.filters = filters;
    }
}
